﻿using System;
using System.IO;
using System.Text;

namespace Common
{
    public class Logger
    {
        private static StreamWriter writer;
        private static readonly string _path;
        private static string _type;
        private static string newLine = "\r\n\r\n";
        private static string errorMessage = "An error occured in the application.\r\nException details:{0}";

        public static string type
        {
            get { return _type; }
            set { _type = value; CreateStream(); }
        }

        static Logger()
        {
            writer = null;
            _path = "C:\\hseps\\logs\\";
        }

        private static void CreateStream()
        {
            if (type == String.Empty)
                type = "default";

            if (!Directory.Exists(_path + _type))
            {
                try
                {
                    var di = Directory.CreateDirectory(_path + _type);
                    System.Diagnostics.Debug.WriteLine(DateTime.Now + " - Directory successfully created");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(DateTime.Now + " - Unable to create log directory, More information: \n" + ex.Message);
                }
            }

            if (!File.Exists(_path + _type + "\\" + DateTime.Now.ToShortDateString() + "_log.txt"))
            {
                try
                {
                    FileStream fs = new FileStream(_path + _type + "\\" + DateTime.Now.ToShortDateString() + "_log.txt", FileMode.Create, FileAccess.ReadWrite);
                    fs.Close();
                    System.Diagnostics.Debug.WriteLine(DateTime.Now + " - File successfully created");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(DateTime.Now + " - Unable to create log file, More information: \n" + ex.Message);
                }
            }
            try
            {
                writer = new StreamWriter(_path + _type + "\\" + DateTime.Now.ToShortDateString() + "_log.txt", true);
                writer.WriteLine();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(DateTime.Now + " - Exception caught while trying to write in log file, More information: \n" + ex.Message);
            }
        }

        public static void Write(string line)
        {
            if (writer == null)
            {
                CreateStream();
            }
            if (writer != null)
            {
                lock (writer)
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now + ":\t" + line);
                        writer.Flush();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(DateTime.Now + " - Exception caught while trying to write in log file, More information: \n" + ex.Message);
                    }
                }
            }
        }

        public static void Write(Exception ex, string additionalInfo = "")
        {
            var sb = new StringBuilder();
            if (additionalInfo != string.Empty)
            {
                sb.Append(additionalInfo + newLine);
            }
            while (ex != null)
            {
                sb.Append("Message: " + ex.Message + newLine);
                sb.Append("Source: " + ex.Source + newLine);
                sb.Append("Target site: " + ex.TargetSite + newLine);
                sb.Append("Stack trace: " + ex.StackTrace + newLine);
                ex = ex.InnerException;
            }

            Write(String.Format(errorMessage, sb));
            //Write((additionalInfo == string.Empty ? "Exception occured in " + ex.Source : additionalInfo) + " More info: " + ex.Message);
            //Write("Stack trace: \r\n" + ex.StackTrace);
        }
    }
}
